package cc.comac.mom.datahandlers.entity;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import cc.comac.mom.bean.EquipPartBorrowDemand;
import cc.comac.mom.bean.EquipPartBorrowDetailBean;
import cc.comac.mom.bean.base.LimitPageBean;
import cc.comac.mom.dao.entity.EquipPartBorrowDemandDao;
import cc.comac.mom.dao.entity.EquipPartBorrowDetailDao;
import cc.comac.mom.entity.EquipPartBorrowDemandEntity;
import cc.comac.mom.util.BaseEntityDataUtil;
import cc.comac.mom.util.ResultBean;

@Component
@Transactional
public class EquipPartBorrowDemandDataHandler extends ServiceImpl<EquipPartBorrowDemandDao, EquipPartBorrowDemandEntity>{

	@Autowired
	private EquipPartBorrowDemandDao equipPartBorrowDemandDao;
	
	@Autowired
	private BaseEntityDataUtil baseEntityDataUtil;
	
	@Autowired
	private EquipPartBorrowDetailDao equipPartBorrowDetailDao;
	
	public ResultBean insertPartBorrowDemand(EquipPartBorrowDemand equipPartBorrowDemand) {
		ResultBean resultBean = new ResultBean(true);
		EquipPartBorrowDemandEntity innerData = equipPartBorrowDemand.getInnerData();
		if (innerData.getApplyQuantity().compareTo(new BigDecimal(0)) == 0) {
			throw new RuntimeException("申请数量不能为空！");
		}else if (StringUtils.isEmpty(innerData.getDevNumber())) {
			throw new RuntimeException("设备编号不能为空");
		} else if (innerData.getSparePartId() == 0) {
			throw new RuntimeException("备件不能为空");
		}
		equipPartBorrowDemandDao.insert(innerData);
		return resultBean;
	}
	
	// 批量新增备件领用数据
	public ResultBean batchInsertPartBorrowDemand(List<EquipPartBorrowDemand> list) {
		ResultBean resultBean = new ResultBean(true);
		if (list.size() > 0) {
			List<EquipPartBorrowDemandEntity> entityList = new ArrayList<EquipPartBorrowDemandEntity>();
			for (EquipPartBorrowDemand equipPartBorrowDemand : list) {
				EquipPartBorrowDemandEntity innerData = equipPartBorrowDemand.getInnerData();
				if (innerData.getApplyQuantity().compareTo(new BigDecimal(0)) != 0) {
					throw new RuntimeException("申请数量不能为空！");
				}else if (StringUtils.isEmpty(innerData.getDevNumber())) {
					throw new RuntimeException("设备编号不能为空");
				} else if (innerData.getSparePartId() == 0) {
					throw new RuntimeException("备件不能为空");
				}
				entityList.add(innerData);
			}
			this.saveBatch(entityList, entityList.size());
		}
		return resultBean;
	}
//
//	/**
//	 * 创建领用单
//	 * **/
//	public ResultBean batchUpdateBorrowDemandStatus(List<EquipPartBorrowDemand> list) {
//		ResultBean resultBean = new ResultBean(true);
//		if (list.size() > 0) {
//			List<Long> idsList = new ArrayList<Long>();
//			for (int i = 0; i < list.size(); i++) {
//				EquipPartBorrowDemandEntity innerData = list.get(i).getInnerData();
//				idsList.add(innerData.getId());
//			}
//			List<EquipPartBorrowDemandEntity> selectBatchIds = equipPartBorrowDemandDao.selectBatchIds(idsList);
//			for (EquipPartBorrowDemandEntity equipPartBorrowDemandEntity : selectBatchIds) {
//				equipPartBorrowDemandEntity.setBorrowDemandStatus("0");
//				baseEntityDataUtil.updateBaseEntityData(equipPartBorrowDemandEntity);
//			}
//			this.updateBatchById(selectBatchIds, selectBatchIds.size());
//		}
//		return resultBean;
//	}
//	
	// 通过条件查询备件领用需求表
	public ResultBean selectBysomeFields(EquipPartBorrowDemand equipPartBorrowDemand) {
		ResultBean resultBean = new ResultBean(true);
		if (equipPartBorrowDemand.getPageNum() == 0) {
			equipPartBorrowDemand.setPageNum(1);
		}
		if (equipPartBorrowDemand.getPageSize() == 0) {
			equipPartBorrowDemand.setPageSize(10);
		}
		EquipPartBorrowDemandEntity innerData = equipPartBorrowDemand.getInnerData();
		Page<EquipPartBorrowDemandEntity> page = new Page<EquipPartBorrowDemandEntity>(equipPartBorrowDemand.getPageNum(),equipPartBorrowDemand.getPageSize());
		QueryWrapper<EquipPartBorrowDemandEntity> queryWrapper = new QueryWrapper<EquipPartBorrowDemandEntity>();
		queryWrapper.eq("row_status", 1);
		queryWrapper.eq("borrow_demand_status", 1);
		queryWrapper.orderByDesc("creation_time");
		if (StringUtils.isNotEmpty(innerData.getRelationNumber())) {
			queryWrapper.like("relation_number", innerData.getRelationNumber());
		}
		if (StringUtils.isNotEmpty(innerData.getDevNumber())) {
			queryWrapper.like("dev_number", innerData.getDevNumber());
		}
		if (StringUtils.isNotEmpty(innerData.getBorrowStyle())) {
			queryWrapper.eq("borrow_style", innerData.getBorrowStyle());
		}
		if (StringUtils.isNotEmpty(innerData.getSparePartNumber())) {
			queryWrapper.like("spare_part_number", innerData.getSparePartNumber());
		}
		if (StringUtils.isNotEmpty(innerData.getSparePartName())) {
			queryWrapper.like("spare_part_name", innerData.getSparePartName());
		}
		if (StringUtils.isNotEmpty(innerData.getSparePartModel())) {
			queryWrapper.like("spare_part_model", innerData.getSparePartModel());
		}
		IPage<EquipPartBorrowDemandEntity> selectPage = equipPartBorrowDemandDao.selectPage(page, queryWrapper);
		LimitPageBean<EquipPartBorrowDemandEntity> of = (LimitPageBean<EquipPartBorrowDemandEntity>) LimitPageBean.of(selectPage);
		resultBean.setObject(of);
		return resultBean;
	}
	
	// 查询备件所在仓库及库存等数据
	public ResultBean selectSpareWarehouseStock(EquipPartBorrowDetailBean equipPartBorrowDetailBean) {
		ResultBean resultBean = new ResultBean(true);
		if (equipPartBorrowDetailBean.getPageNum() == 0) {
			equipPartBorrowDetailBean.setPageNum(1);
		}
		if (equipPartBorrowDetailBean.getPageSize() == 0) {
			equipPartBorrowDetailBean.setPageSize(10);
		}
		equipPartBorrowDetailBean.setPageNum((equipPartBorrowDetailBean.getPageNum()-1)*equipPartBorrowDetailBean.getPageSize());
		List<EquipPartBorrowDetailBean> selectSpareWarehouseStock = equipPartBorrowDetailDao.selectSpareWarehouseStock(equipPartBorrowDetailBean);
		long selectCountSpareWarehouseStock = equipPartBorrowDetailDao.selectCountSpareWarehouseStock(equipPartBorrowDetailBean);
		LimitPageBean<EquipPartBorrowDetailBean> limitPageBean = new LimitPageBean<EquipPartBorrowDetailBean>();
		limitPageBean.setPageNum(equipPartBorrowDetailBean.getPageNum());
		limitPageBean.setPageSize(equipPartBorrowDetailBean.getPageSize());
		limitPageBean.setPageCount((int)Math.ceil(selectCountSpareWarehouseStock* 1.0d/equipPartBorrowDetailBean.getPageSize()));
		limitPageBean.setTotalQuantity((int)selectCountSpareWarehouseStock);
		limitPageBean.setEntityList(selectSpareWarehouseStock);
		resultBean.setObject(limitPageBean);
		return resultBean;
	}
	
	
	
	
}
